package com.android.delivery.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.android.delivery.App;
import com.android.delivery.entity.PushInfo;
import com.android.delivery.util.GsonUtil;
import com.android.delivery.util.SoundUtil;

import java.util.Map;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        Log.e("MyMessageReceiver", "onNotification" + "title: " + title + ", content:" + summary);

        PushInfo pushInfo = GsonUtil.parse(summary, PushInfo.class);
        dispenseMessageInfo(pushInfo);
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {

        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    private void dispenseMessageInfo(PushInfo pushInfo) {
        if (pushInfo != null) {
            if (pushInfo.type == PushType.TYPE_ORDER_RECE) {
                PushNotifyManager.getInstance().dispatchOrderRece();
                SoundUtil.getInstance().playTick();
            }
        }
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }

    @Override
    public Notification customNotificationUI(Context context, Map<String, String> map) {
        Log.e("MyMessageReceiver", "Receive customNotificationUI, title: " + map.toString());

        String title = map.get("title");
        String desc = map.get("content");
        PushInfo pushInfo = GsonUtil.parse(desc, PushInfo.class);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        return PushNotificationUtil.getInstance().createPushNotification(context, manager, PushNotificationUtil.PUSH_CHANNEL_ID, PushNotificationUtil.PUSH_CHANNEL_NAME, title, pushInfo.content, 0);
    }
}